package cn.itcast.Controller;

import cn.itcast.domain.Kind;
import cn.itcast.utils.Helper;


import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
@Controller
@RequestMapping("qq")
public class controller implements Serializable{
    @RequestMapping("/io")
    public String ttst(){
        System.out.println("yyyy");
        return "success";
    }
    @RequestMapping("/aj")
    public @ResponseBody Kind ttest1(@RequestBody Kind kind){
        System.out.println(kind);
        kind.setId(9);
        kind.setKind_name("art");
        return kind;
    }
    @RequestMapping("/upload")
    public String ttsto2(HttpServletRequest request, MultipartFile upload) throws IOException {

        String realPath = request.getSession().getServletContext().getRealPath("/upload/");
        File file=new File(realPath);
        file.mkdir();

        //重命名上传文件
        String oldName = upload.getOriginalFilename();
        String newName = Helper.getDate(oldName);

        //文件上传
        upload.transferTo(new File(realPath,newName));

        return "success";
    }

    @RequestMapping("/upload1")
    public String ttsto3( MultipartFile upload) throws IOException {

        String path="http://localhost:9090/uploads/";


        //重命名上传文件
        String oldName = upload.getOriginalFilename();
        String newName = Helper.getDate(oldName);
        Client client = Client.create();
        WebResource resource = client.resource(path + newName);
       resource.put(upload.getBytes());


        return "success";
    }

    @RequestMapping("/ooo")
    public String ttt(){
            /*int i=1/0;*/
        System.out.println("controller里面的");
        return "success";
    }

}
